﻿using UnityEngine;
using System.Collections;


public class GameplayController : MonoBehaviour
{

    #region Items Var
    GameObject[]    enemyCar;
    GameObject      fuelCar;
    GameObject      oil;
    GameObject      barrier;

    public float p_fMoveDirection = -1; //bg move from top to bottom

    GameObject bottomWall, leftWall, rightWall;

    #endregion


    #region CoreGameplay
    // Use this for initialization
	void Start () {
	    
	}

	
	// Update is called once per frame
	void Update () {

        // Handle user input
        InputManager.HandleInput();

    }

    /// <summary>
    /// Init all nessesary items
    /// </summary>
    void InitData()
    {

        // Create all object
        CreateEnemy();      // as 3
        CreateFuelCar();    // as 4
        CreateOil();        // as 5
        CreateBarrier();    // as 6
    }

    
    void CreateEnemy()
    {
        enemyCar = new GameObject[GLOBAL.MAX_ENEMY];
        int i = 0;
        for (i = 0; i < GLOBAL.MAX_ENEMY; i++)
        {
            enemyCar[i] = Instantiate(Resources.Load("Enemy")) as GameObject;
        }
    }

    void CreateFuelCar()
    { 
        fuelCar = new GameObject();
        fuelCar = Instantiate(Resources.Load("FuelCar")) as GameObject;
    }

    void CreateOil()
    {
        oil = new GameObject();
        oil = Instantiate(Resources.Load("Oil")) as GameObject;
    }

    void CreateBarrier()
    {
        barrier = new GameObject();
        barrier = Instantiate(Resources.Load("Barrier")) as GameObject;
    }

    #endregion

    #region CollisionWall
    void CreateCollisionWall()
    {
        bottomWall = Instantiate(Resources.Load("CollisionWall")) as GameObject;
        leftWall = Instantiate(Resources.Load("CollisionWall")) as GameObject;
        rightWall = Instantiate(Resources.Load("CollisionWall")) as GameObject;

        // Place collision wall to the correct place
    }


    #endregion


    #region Items manage
    void MakeRandomItems()
    {

        

    }

    bool CheckIfSamePlace(GameObject target, GameObject obj)
    {
        Transform startPos, endPos;


        startPos = target.transform;
        endPos   = obj.transform;

        //endPos.position = new Vector3(endPos.position.x + obj.renderer.bounds.size.x

        
        // Check collision
        // tar.pos.x <= obj.x <= tar.pos.x + tar.size.x
        // tar.pos.y <= obj.y <= tar.pos.y + tar.size.y
        // true

        if (startPos.position.x <= obj.transform.position.x)
        {
            return true;
        }

        

        return false;
    }

    #endregion

    #region Repeat Items

    void PlaceItems()
    {

    }

    #endregion




    #region BG Scrolling

    void MakeBGScroll()
    {
        // Move up
        transform.Translate(new Vector3(0, GLOBAL.s_fSpeed * p_fMoveDirection * Time.deltaTime, 0));

    }
    #endregion

    #region Speed change

    void RestartCar()
    {
        GLOBAL.s_fSpeed = 1.0f;
    }

    void StopCar()
    {
        GLOBAL.s_fSpeed = 0.0f;
    }


    void SpeedUp()
    {
        GLOBAL.s_fSpeed += GLOBAL.s_fUpStep;
    }

    void HandleSpeed()
    {
        // Make bg move faster over times
        SpeedUp();
    }

    #endregion



    


}

